package com.dmap.knowledge.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.controller.api.param.BannerHistoryParam;
import com.dmap.knowledge.service.ArticleHistoryService;
import com.dmap.knowledge.service.BannerExtensionService;
import com.dmap.knowledge.service.BannerHistoryService;
import com.dmap.knowledge.service.BannerService;
import com.dmap.knowledge.vo.AppBannerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api(value = "轮播图", description = "App/轮播图")
@RestController
@RequestMapping(value = "/api/dmap-knowledge-server/v1/")
public class BannerController {
    
    @Resource
    private BannerService bannerService;
    @Resource
    private BannerHistoryService bannerHistoryService;
    @Resource
    private BannerExtensionService bannerExtensionService;
    @Resource
    private ArticleHistoryService articleHistoryService;
    
    @ResponseBody
    @ApiOperation(value = "获取轮播图", notes = "获取轮播图")
    @RequestMapping(value = "banner", method = RequestMethod.GET)
    public BaseVo selectBanner(@RequestHeader(value = "language") String lang, @RequestParam String projectId) {
        BaseVo result = new BaseVo();
        if (lang == null || projectId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang) || StringUtils.isBlank(projectId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<AppBannerVo> appBannerVos = bannerExtensionService.selectAppBanner(lang, projectId);
        result.setData(appBannerVos);
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "增加轮播图历史", notes = "增加轮播图历史")
    @RequestMapping(value = "/banner/insert/history", method = RequestMethod.POST)
    public BaseVo selectBannerHistory(@RequestBody BannerHistoryParam bannerHistoryParam) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(bannerHistoryParam.getUserId()) || bannerHistoryParam.getType() == 0 || StringUtils.isBlank(bannerHistoryParam.getJumpId())  ||
                !StringUtils.isNumeric(bannerHistoryParam.getUserId()) || !StringUtils.isNumeric(bannerHistoryParam.getJumpId())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success;
        if(StringUtils.isBlank(bannerHistoryParam.getBannerId())||!StringUtils.isNumeric(bannerHistoryParam.getBannerId())){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        success = bannerHistoryService.insertBannerHistory(bannerHistoryParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }
}
